package cn.xshi.file.web;

import cn.xshi.common.annotation.Auth;
import cn.xshi.common.base.BaseAction;
import cn.xshi.common.base.BasePage;
import cn.xshi.common.base.BaseResult;
import cn.xshi.common.entity.OauthAccountEntity;
import cn.xshi.common.util.StringUtil;
import cn.xshi.file.model.Attachment;
import cn.xshi.file.param.AttachmentForm;
import cn.xshi.file.service.AttachmentService;
import com.github.pagehelper.PageInfo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.*;
import javax.annotation.Resource;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
/**
* 附件管理 
* 2015-05-24 08:36:53  邓纯杰
*/
@RestController
@RequestMapping("/attachment")
@Api(value = "平台附件API",tags = "平台附件API",description = "平台附件API")
public class AttachmentController extends BaseAction {

	@Resource
	private AttachmentService attachmentService;

	/**
	 * 查询附件列表并分页
	 * @param attachmentForm
	 * @param pageNo
	 * @param pageSize
	 * @return
	 */
	@ApiOperation(value="查询附件列表并分页", notes="查询附件列表并分页")
	@GetMapping(value="/list")
	@Auth(value = "/attachment/list",authenticationType = Auth.AuthorizationType.REQUIRED_LOGIN_NOT_AUTH)
	public BasePage<List<Attachment>> getAttachmentListByCondition(cn.xshi.file.form.AttachmentForm attachmentForm,Integer pageNo, Integer pageSize){
		commonHPagerByPageNo(pageNo,pageSize);
		List<Attachment> attachmentList = attachmentService.getAttachmentListByCondition(attachmentForm);
		for(Attachment attachment : attachmentList){
			if(!StringUtil.isEmpty(attachment.getCreateId())){
				OauthAccountEntity createBy = getAccount(attachment.getCreateId());
				if(null != createBy){
					attachment.setCreateBy(createBy.getName());
				}
			}
		}
		PageInfo<Attachment> page = new PageInfo<Attachment>(attachmentList);
		return outPageBootByPageNo(page,pageNo,pageSize);
	}

	/**
	 * 查询单个附件
	 * @param id
	 */
	@ApiOperation(value="查询单个附件", notes="查询单个附件")
	@GetMapping(value="/get/{id}")
	@Auth(value = "/attachment/get",authenticationType = Auth.AuthorizationType.REQUIRED_LOGIN_NOT_AUTH)
	public BaseResult<Attachment> getAttachmentById(@PathVariable("id")String id){
		Attachment attachment = attachmentService.getAttachmentById(id);
		if(!StringUtil.isEmpty(attachment.getCreateId())){
			OauthAccountEntity createBy = getAccount(attachment.getCreateId());
			if(null != createBy){
				attachment.setCreateBy(createBy.getName());
			}
		}
		return outDataStr(attachment);
	}

	/**
	 * 批量查询附件
	 * @param id
	 */
	@ApiOperation(value="批量查询附件", notes="批量查询附件")
	@GetMapping(value="/list/{id}")
	@Auth(authenticationType = Auth.AuthorizationType.REQUIRED_LOGIN_NOT_AUTH)
	public BaseResult<List<Attachment>> getAttachmentList(@PathVariable("id")String id){
		Map<String,Object> condition = new HashMap<>();
		condition.put("id", id.split(","));
		List<Attachment> list = attachmentService.getAttachmentList(condition);
		return outDataStr(list);
	}

	/**
	 * 添加
	 * @param attachment
	 */
	@PostMapping(value="/add")
	@ApiOperation(value="创建附件", notes="创建附件")
	@Auth(value = "/attachment/add")
	public BaseResult addAttachment(@RequestBody Attachment attachment){
		int i = 0;
		if(null != attachment){
			attachment.setId(toUUID());
			i= attachmentService.addAttachment(attachment);
		}
		if(i>0){
			return outAudStr(true);
		}else{
			return outAudStr(false);
		}
	}

	/**
	 * 修改
	 * @param attachment
	 */
	@ApiOperation(value="编辑附件", notes="编辑附件")
	@PutMapping(value="/update")
	@Auth(value = "/attachment/update")
	public BaseResult updateAttachment(@RequestBody Attachment attachment){
		int i = 0;
		if(null != attachment){
			i= attachmentService.updateAttachment(attachment);
		}
		if(i>0){
			return outAudStr(true);
		}else{
			return outAudStr(false);
		}
	}

	/**
	 * 删除
	 * @param id
	 */
	@ApiOperation(value="删除附件", notes="删除附件")
	@DeleteMapping(value="/delete")
	@Auth(value = "/attachment/delete")
	public BaseResult delAttachment(String id){
		int i = 0;
		if(!StringUtil.isEmpty(id)){
			Map<String, Object> condition = new HashMap<String, Object>();
			condition.put("id",id.split(","));
			i= attachmentService.delAttachment(condition);
		}
		if(i>0){
			return outAudStr(true);
		}else{
			return outAudStr(false);
		}
	}

	/**
	 * 批量查询附件
	 * @param attachmentForm
	 */
	@ApiOperation(value="批量查询附件", notes="批量查询附件")
	@PostMapping(value="/batch/list")
	@Auth(value = "/attachment/batch/list",authenticationType = Auth.AuthorizationType.REQUIRED_LOGIN_NOT_AUTH)
	public BaseResult<List<Attachment>> getBatchAttachmentList(@RequestBody AttachmentForm attachmentForm){
		if(StringUtil.isEmpty(attachmentForm.getId())){
			return BaseResult.fail("未能获取到附件编号");
		}
		Map<String,Object> condition = new HashMap<>();
		condition.put("id", attachmentForm.getId().split(","));
		List<Attachment> list = attachmentService.getAttachmentList(condition);
		return BaseResult.success(list);
	}
}
